package com.smallA.controller;

import com.smallA.constant.AccessConstant;
import com.smallA.constant.DataConstant;
import com.smallA.po.Client;
import com.smallA.po.Exam;
import com.smallA.po.Grade;
import com.smallA.service.ExamService;
import com.smallA.service.GradeService;
import com.smallA.service.TestService;
import com.smallA.service.UserService;
import com.smallA.utils.AutoSubmit;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 君未洋
 */
@Controller
public class StudentController {
    private UserService userService;
    private GradeService gradeService;
    private TestService testService;
    private ExamService examService;

    public void setExamService(ExamService examService) {
        this.examService = examService;
    }

    public void setTestService(TestService testService) {
        this.testService = testService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }


    public void setGradeService(GradeService gradeService) {
        this.gradeService = gradeService;
    }


    /**
     * 这个就是单纯的查看所有的考试了
     *
     * @param model 必要参数
     * @return 返回所有查询到的考试
     */
    @Secured(AccessConstant.TEACHER_ACCESS)
    @RequestMapping("/Student/getTests")
    @ResponseBody
    public Object getTests(Model model) {
        /*model.addAttribute(DataConstant.ALL_TEST, testService.selectAllTest());
        return "student/allTest";*/
        return testService.selectAllTest();
    }

    /**
     * 通过考试id返回剩余的考试时间的毫秒值
     *
     * @param gid
     * @return
     */
    @RequestMapping("/Student/getEndTime")
    @ResponseBody
    public Long getEndTime(Integer gid) {
        if (gid == null) {
            return null;
        }
        Grade grade = gradeService.selectGradeById(gid);
        return (grade.getBeginTime().getTime() + grade.getTest().getTestTime() * 1000 * 60 - System.currentTimeMillis());
    }

    /**
     * 通过学生id和试卷id报名参加考试
     *
     * @param sid 考生id
     * @param tid 试卷id
     * @return
     */
    @ResponseBody
    @RequestMapping("/Student/joinExam")
    public Object joinExam(Integer sid, Integer tid) {
        return gradeService.insertGrade(sid, tid);
    }


    /**
     * 开始考试用的接口
     *
     * @param gid
     * @param eid
     * @param model
     * @return
     */
    // @Secured(AccessConstant.STUDENT_ACCESS)
    @ResponseBody
    @RequestMapping("/Student/beginExam")
    public Object beginExam(Integer gid, Integer eid, Model model) {
        if (gradeService.BeginGrade(gid)) {
            //if判断来开始考试,考试获取对应的数据上传到Model
            Exam exam = examService.selectExamByExamId(eid);
            //获取考试信息上传
           /* model.addAttribute(DataConstant.EXAM_DETAIL, exam);
            model.addAttribute(DataConstant.ID_GRADE, gid);
            return "student/examination";*/
            Map<String, Object> map = new HashMap<>();
            map.put(DataConstant.EXAM_DETAIL, exam);
            map.put(DataConstant.ID_GRADE, gid);
            return map;
        } else {
            /*model.addAttribute(DataConstant.MSG, "未到开始考试时间,请准时参考");
            return "/student/other";*/
            return false;
        }
    }

    /**
     * 提交试卷用的接口
     *
     * @param sid
     * @param gid
     * @param req
     * @return
     */
    @RequestMapping("/Student/winding")
    // @Secured(AccessConstant.STUDENT_ACCESS)
    @ResponseBody
    public Object endExam(Integer sid, Integer gid, HttpServletRequest req) {
        Map map = req.getParameterMap();
        testService.windingTest(map, sid, gid);
        /*if (gradeService.EndGrade(gid)) {
            req.setAttribute(DataConstant.MSG, "提交成功,请等待老师批改");
        } else {
            req.setAttribute(DataConstant.MSG, "提交失败,请重新尝试提交");
        }
        return "student/other";*/
        return gradeService.EndGrade(gid);
    }

    /**
     * 通过学生的id获取学生对应的考试情况
     *
     * @param sid 学生id
     */
    // @Secured(AccessConstant.STUDENT_ACCESS)
    @ResponseBody
    @RequestMapping("/Student/getGrade")
    public Object getGrade(Integer sid, Model model) {
        /*List<Grade> grades = gradeService.selectGradesById(sid);
        model.addAttribute(DataConstant.EXAM_ALL_STUDENT, grades);
        //上传对应的学生数据,然后跳转到exam.jsp
        return "/student/exam";*/
        return gradeService.selectGradesById(sid);
    }

    /**
     * 获取考试的详细信息
     *
     * @return 返回考试的集合
     */
    @ResponseBody
    @RequestMapping("/Student/getExam")
    //@Secured(AccessConstant.STUDENT_ACCESS)
    public Object getExam(Integer gid, Model model) {
        /*Exam exam = examService.selectExamById(gid);
        model.addAttribute(DataConstant.EXAM_DETAIL, exam);
        return "student/test";*/
        return examService.selectExamById(gid);
    }

    /**
     * 学生登录基本界面,用来登录后上传数据用
     *
     * @param session
     * @return
     */
    @RequestMapping("/Student/goHome")
    public String goHome(HttpSession session) {
        //从后台获取认证后的用户名然后上传数据
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        Client client = userService.selectUserByName(name);
        session.setAttribute(DataConstant.STUDENT_USER, client);
        return userService.judge(client);
    }

    @RequestMapping("/Student/task")
    @ResponseBody
    public String taskEmail(String address, Integer time) {
        AutoSubmit autoSubmit = new AutoSubmit(time, address);
        //
        if (address != null) {
            autoSubmit.setMsg("亲爱的用户,您发送的测试邮件已到达");
            new Thread(autoSubmit).start();
        }
        return "Message sending";
    }



}
